package com.dps.broadvision.visitor.client;

import java.util.HashMap;
import java.util.ResourceBundle;

import com.broadvision.visitor.client.Visitor;
import com.broadvision.visitor.client.VisitorManager;
import com.broadvision.visitor.exceptions.VisitorException;
import com.broadvision.visitor.exceptions.VisitorExistsException;
import com.broadvision.visitor.interfaces.ExternalProfileAccessor;
import com.broadvision.visitor.server.ProfileDaoUtil;
import com.broadvision.visitor.utils.ExternalDataConfig;
import com.broadvision.visitor.utils.ExternalDataConfigUtil;

public class ExternalVisitorManager extends VisitorManager {

	 public ExternalVisitorManager()
	 {
	      super();
	 }
	 /**************************************************************************/
	 public Visitor createNewVisitor(String userName, String password)  throws VisitorExistsException, VisitorException
     {
		 System.out.println("Going to create external visitor object");
		 Visitor newVisitor = null;
		 newVisitor = createVisitor(userName, password);
		 String extDataSrcs[] = ExternalDataConfigUtil.getProfileDataSources();
		 if(extDataSrcs == null)
			 return newVisitor;
		 int looper = 0;
		 int numberOfDataSrcs = extDataSrcs.length;
		 ExternalDataConfig extDataCfg = null;
		 for(looper = 0; looper < numberOfDataSrcs; looper++)
		 {
			 extDataCfg = ExternalDataConfigUtil.getExternalProfileConfigByName(extDataSrcs[looper]);
			 String extTableName = extDataCfg.getExternalTableName();
			 String dn = null;
			 String dataSourceName = extDataCfg.getDataSourceName();
			 try
			 {
				 ExternalProfileAccessor extProfileAccessor = ProfileDaoUtil.getExternalProfileAccessor(extTableName, "BV_DEFAULT");
				 HashMap hashMap = buildUserAttributeSet(userName, password, extDataCfg);
				 dn = (String)hashMap.get("DN");
				 extProfileAccessor.createUser(hashMap);
			 }
			 catch(Exception ex)
			 {
				 continue;
			 }
			 try
			 {
				 mapLdapUser(userName, dataSourceName, dn);
			 }
			 catch(Exception ex) { }
		 }
		 return newVisitor;
     }
	 /**************************************************************************/
	 private HashMap buildUserAttributeSet(String userName, String password, ExternalDataConfig extDataCfg)
	 {
		 try
		 {
	    	ResourceBundle rb = ResourceBundle.getBundle("LDAPSettings");
	    	String objectClasses = rb.getString("ldap.user.object.classes");
	    	String objectClass[] = objectClasses.split(",");
			HashMap hashMap = new HashMap();
	        String userKey = extDataCfg.getUserKey();
	        String userBase = extDataCfg.getUserBase();
	        StringBuffer userDn = new StringBuffer(userKey);
	        userDn.append("=").append(userName).append(",");
	        userDn.append(userBase);
	        hashMap.put(rb.getString("ldap.user.object.dn"), userDn.toString());
	        //hashMap.put("OBJECTCLASS", new String[] {"organizationalPerson","top"});
	        hashMap.put("OBJECTCLASS", objectClass);
	        hashMap.put(rb.getString("ldap.user.object.cn"), userName);
	        hashMap.put(rb.getString("ldap.user.object.sn"), userName);
	        //hashMap.put("sAMAccountName", userName);
	        hashMap.put(rb.getString("ldap.user.object.password"), password);
	        return hashMap;
       }
       catch(Exception exc){
     	return null;
      }
	 }
	 /**************************************************************************/
}
